<?php


/**
 * 获取图片
 * @param
 * @return  string
 */
function thumb($img = '', $width = 0, $height = 0) {
    if (empty($img)) {
        return __ROOT__ . '/Uploads/nopic.gif';
    }
    $Uploads = '/Uploads/';
    $file = '.' . $Uploads . $img;
    if (file_exists($file)) {
        if (empty($width)) {
            return __ROOT__ . substr($file, 1);
        } else {
            $pathinfo = pathinfo($file);
            $thumb_file = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '_' . $width . '-' . $height . '.' . $pathinfo['extension'];
            if (file_exists($thumb_file)) {
                return __ROOT__ . substr($thumb_file, 1);
            } else {
                $image = new \Think\Image();
                $image->open($file);
                if (empty($height)) {
                    $height = $image->height();
                }
                $image->thumb($width, $height,\Think\Image::IMAGE_THUMB_CENTER)->save($thumb_file);
                return __ROOT__ . substr($thumb_file, 1);
            }
        }
    }
    return __ROOT__ . '/Uploads/nopic.gif';
}


 /**
  * 检测输入的验证码是否正确
  * @param  string  $code 为用户输入的验证码字符串
  * @return bool
  */
 function check_verify($code, $id = ''){
     $verify = new \Think\Verify();
     return $verify->check($code, $id);
 }

/**
 * 获取单页内容
 * @param  int  $catid
 * @return  string
 */
function get_page($catid = 0){
    if ($catid) {
        return M('Pages')->where(array('catid'=>$catid))->getField('content');
    } else {
        return '';
    }
}

/**
 * 获取下级子分类id
 * @param  [type] $catid [description]
 * @return [type]        [description]
 */
function get_childs($catid){
	$cates = category_tree($catid);
	if ($cates) {
		$str = '';
		foreach($cates as $key => $val){
			$str .= $val['id'] . ',';
			//$str .= get_childs($key);
		}
        $str .= $catid . ',';
		return $str;
	} else {
        $str = $catid . ',';
		return $str;
	}
}

/**
 * 获取下级栏目
 * @param   int       $catid  栏目catid
 * @return  array
 */
function category_tree($catid = 0) {
    $data = array();
    $array = D('Category')->where(array('pid'=>$catid))->order('sort asc,id desc')->select();
    if(!empty($array)){
        foreach ($array as $key=>$val) {
            $data[$key] = $val;
            if(!empty($val['siteurl'])){
              $data[$key]['url']=$val['siteurl'];
            }else{
              $data[$key]['url']=U($val['type'],array('catid'=>$val['id']));
            }
        }
    }else{
        //$smalltree=D('Category')->where('id='.$catid)->find();
        $data[0] = $smalltree = D('Category')->find($catid);
        if(!empty($smalltree['siteurl'])){
          $data[0]['url'] = $val['siteurl'];
        }else{
          $data[0]['url'] = U($smalltree['type'],array('catid'=>$smalltree['id']));
        }
    }
    return $data;
}

/**
 * 单纯的获取下级栏目
 * @param   int       $catid  栏目catid
 * @return  array
 */
function category_tree2($catid = 0) {
    $data = array();
    $array = D('Category')->where(array('pid'=>$catid,'display'=>1))->order('sort asc,id desc')->select();
    foreach ($array as $key=>$val) {
        $data[$key] = $val;
        if(!empty($val['siteurl'])){
           $data[$key]['url']=$val['siteurl'];
        }else{
           $data[$key]['url']=U($val['type'],array('catid'=>$val['id']));
        }

    }
    return $data;
}

/**
 * 单纯的获取导航下级栏目
 * @param   int       $catid  栏目catid
 * @return  array
 */
function nav_tree($catid = 0) {
    $data = array();
    $array = D('Category')->where(array('pid'=>$catid,'nav_display'=>1))->order('sort asc,id desc')->select();
    foreach ($array as $key=>$val) {
        $data[$key] = $val;
        if(!empty($val['siteurl'])){
          $data[$key]['url']=$val['siteurl'];
        }else{
          $data[$key]['url']=U($val['type'],array('catid'=>$val['id']));
        }

    }
    return $data;
}

/**
 * 获取广告位
 * @param   int         $id   广告位id
 * @param   int/string  $limit   数量
 * @return  array
 */
function get_ads($catid = 0, $limit = 0){
    if (empty($catid)) {
        return array();
    } else {
        $model = D('Ads');
        $map = array();
        $map['catid'] = $catid;
        $map['startdate']=array('ELT',time());
        $map['enddate']=array('EGT',time());

        if (empty($limit)) {
            $limit = '';
        }
        $lists = $model->where($map)->order('sort asc, id desc')->limit($limit)->select();
        if ($lists) {
            return $lists;
        } else {
            return array();
        }
    }
}


/**
 * 获取友情链接
 * @param   int         $id   友链版位id
 * @param   int/string  $limit   数量
 * @return  array
 */
function get_links($catid = 0, $limit = 0){
    if (empty($catid)) {
        return array();
    } else {
        $model = D('Links');
        $map = array();
        $map['catid'] = $catid;
        $map['startdate']=array('ELT',time());
        $map['enddate']=array('EGT',time());

        if (empty($limit)) {
            $limit = '';
        }
        $lists = $model->where($map)->order('sort asc, id desc')->limit($limit)->select();
        if ($lists) {
            return $lists;
        } else {
            return array();
        }
    }
}

/**
 * 获取列表
 * @return  array
 */
function get_lists($Model = '',$where = array(),$field = '', $limit = ''){
    if ($Model == 'Article'){
        $sort = 'sort asc,create_time desc,id desc';
        $field = $field . ',id,catid';
    }else{
        $sort = 'sort asc,id desc';
    }

    $Model = M($Model);
    $lists = $Model->where($where)->field($field)->order($sort)->limit($limit)->select();
    foreach ($lists as $key=>$val){
        $lists[$key]['url'] = U('view',array('catid'=>$val['catid'],'id'=>$val['id']));
    }
    return $lists;

}

/**
 * 当前路径
 * 返回指定栏目路径层级
 * @param $catid 栏目id
 * @param $ext 栏目间隔符
 */
function catpos($catid = 0, $ext = ' &gt; ',$index=1) {
    $category = D('Category')->field('title,id,pid')->where(array('id'=>$catid,'display'=>1))->find();
    $html = '';
    if ($catid == 0) {
        $html = '<a href="'. U('Index/index') .'">网站首页</a>' . $html;
        return $html;
    } else {
        if($index==1){
            $html = $ext . '<span>' . $category['title'] . '</span>' . $html;
        }else{
            $html = $ext . '<a href="'.U($category['type'],array('catid'=>$category['id'])).'">' . $category['title'] . '</a>' . $html;
        }
        $index++;
        $html = catpos($category['pid'], $ext,$index) . $html;
    }
    return $html;
}


/*获取网站导航栏目*/
function get_Navigation($catid = 0){
    $nav_list = M('Category')->where(array('pid'=>0,'nav_display'=>1))->order('sort asc,id desc')->select();
    foreach ($nav_list as $key=>$val){
        $nav_list[$key] = $val;
        /*是否启用站外链接*/
        if(!empty($val['siteurl'])){
            $nav_list[$key]['url'] = $val['siteurl'];
        }else{
            $nav_list[$key]['url'] = U($val['type'],array('catid'=>$val['id']));
        }
        $nav_list[$key]['childs'] = nav_tree($val['id']);
        foreach($nav_list[$key]['childs'] as $ko=>$vo){
            if(!empty($vo['siteurl'])){
                $nav_list[$key]['childs'][$ko]['url'] = $vo['siteurl'];
            }else{
                $nav_list[$key]['childs'][$ko]['url'] = U($vo['type'],array('catid'=>$vo['id']));
            }

        }
        if ($catid == $val['id']){
            $nav_list[$key]['active'] = 1;
        }else{
            $nav_list[$key]['active'] = 0;
        }
      }
    return $nav_list;
}


/**
 * 获取顶级分类
 * @param  integer $catid [description]
 * @return [type]         [description]
 */
function get_topinfo($catid = 0){
    if(empty($catid)){return array();exit;}
    $Category = D('Category')->find($catid);
    if($Category['pid'] != 0){ return get_topinfo($Category['pid']);}
    return $Category;
}


function KFTool(){
    $Config = D('Config')->find(1);
    $KF_title = explode(",",$Config['qq_tit']);$KF_number = explode(",",$Config['qq_num']);
    $test=array("KF_title"=>"qq_tit","KF_number"=>"qq_num");$KFTool = array();
    for($i=0;$i<count($KF_title);$i++)
    {
        foreach($test as $key=>$value){
             $KFTool[$i][$value] = ${$key}[$i];
        }
    }
    return $KFTool;
}
